package com.jwdf.uda.ui.adapter;

import android.content.Context;
import android.databinding.DataBindingUtil;
import android.view.View;
import android.view.ViewGroup;

import com.jwdf.uda.R;
import com.jwdf.uda.databinding.ItemSearchRecordBinding;


/**
 * Created by Administrator on 2016/5/20.
 */
public class SearchRecordAdapter extends AbsAdapter<String> {
    private OnRecordItemClickListener mOnRecordItemClickListener;

    public SearchRecordAdapter(Context context) {
        super(context);
    }

    public void setOnRecordItemClickListener(OnRecordItemClickListener listener) {
        this.mOnRecordItemClickListener = listener;
    }

    @Override
    protected AbsViewHolder onCreateViewHolder(ViewGroup parent, int itemType) {
        return new RecordItemViewHolder(mInflater.inflate(R.layout.item_search_record, parent, false));
    }

    public class RecordItemViewHolder extends AbsViewHolder<String> {
        private ItemSearchRecordBinding mBinding;

        public RecordItemViewHolder(View convertView) {
            super(convertView);
            mBinding = DataBindingUtil.bind(convertView);
        }

        @Override
        protected void onBindView(String item) {
            mBinding.tvRecName.setText(item);
            mBinding.ivDelRecord.setTag(R.id.tag, item);
            mBinding.ivDelRecord.setOnClickListener(createOnClickListener());
        }

        private View.OnClickListener createOnClickListener() {
            return new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (mOnRecordItemClickListener != null) {
                        mOnRecordItemClickListener.onItemClick((String) v.getTag(R.id.tag));
                    }
                }
            };
        }
    }

    public interface OnRecordItemClickListener {
        void onItemClick(String item);
    }
}
